/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#include "StdAfx.h"


#include "Tile.h"
#include "../Kernel.h"

extern Kernel* g_kernel;

#define TIME (g_kernel->GetTime())
#define RUN_SCRIPT( str ) (g_kernel->GetScriptEngine()->CallFunction( str ))

Tile::Tile(void)
{
    m_row = 1;
    m_col = 1;

    m_x = 0;
    m_y = 0;

    m_animation.Set( 0, 1, 1, 1 );
}

/**
===============================================================================
Tile Def
===============================================================================
*/
int Tile::Init(Image image)
{
    m_image = image;

    m_width  = image.width;
    m_height = image.height;

    m_x = 0;
    m_y = 0;

    return 0;
}

/** Update the animation */
void Tile::Update(long dt)
{
    m_animation.Update( dt );
}

/** Render the tile */
void Tile::Render( IRender *r, ICamera* cam  )
{
    r->DrawSubImage( &m_image, m_x, m_y, m_animation.GetRow(), m_animation.GetFrame() );
}

/** Add an event */
void Tile::AddEvent( const Vector2f &v, TileEvent &evnt ) 
{ 
    std::pair<int, int> pair;
    pair.first  = v.x;
    pair.second = v.y;

    m_events[pair] = evnt;
}


/** Trigger the event */
void Tile::Trigger( IEntity* triggerer, const Vector2f &v  )
{
    std::pair<int, int> pair;
    pair.first  = v.x;
    pair.second = v.y;
    
    if ( m_events.find( pair ) != m_events.end() )
    {   
        TileEvent &evnt = m_events[pair]; 
        if ( evnt.status == OFF_TILE ) {
            evnt.status = ON_TILE;
            evnt.nextActiveTime = TIME + evnt.waitTime;

            if ( evnt.destination.Length() )
            {
                triggerer->MoveTo( evnt.destination  );
                evnt.status = OFF_TILE;
            }
            if ( evnt.damage )
                triggerer->Damage( evnt.damage );

            if ( evnt.script != "" ) {
                RUN_SCRIPT( evnt.script );
                g_kernel->LPrintf("In C++::After script!");
            }

        } 
        if ( TIME > evnt.nextActiveTime )
        {
            evnt.status = OFF_TILE;
        }
        
    }
}


Tile::~Tile(void)
{
}
